﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using CuaHangLego.Model;
using System.Data.SqlClient;
using CuaHangLego.Bussiness.Parser;

namespace CuaHangLego.DataAccess
{
    public class SanPhamDA : DABase, IDA<SanPham>
    {

        public static SanPhamDA Instance = new SanPhamDA();

        public int Insert(SanPham item)
        {
            throw new NotImplementedException();
        }
        public bool Update(SanPham item)
        {
            throw new NotImplementedException();
        }

        public bool Delete(SanPham item)
        {
            throw new NotImplementedException();
        }

        public IList<SanPham> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM SANPHAM WHERE {0}={1}", "IsVisible", 1);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<SanPham> list = new List<SanPham>();
            while (reader.Read())
            {
                list.Add(SanPhamParser.Parse(reader));
            }
            CloseDB();
            return list;
        }

        public IList<SanPham> GetByTheme(int ID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM SANPHAM WHERE {0}={1} AND {2}={3}", "ChuDeID", ID, "IsVisible", 1);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<SanPham> list = new List<SanPham>();
            while (reader.Read())
            {
                list.Add(SanPhamParser.Parse(reader));
            }
            CloseDB();
            return list;
        }

        public int GetQuantity(int ID)
        {
            int quantity = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT SoLuong FROM SANPHAM WHERE ID={0}", ID);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            while (reader.Read())
            {
                quantity = int.Parse(reader[0].ToString());
                break;
            }
            CloseDB();
            return quantity;
        }

        public SanPham GetByID(int ID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM SANPHAM WHERE {0}={1}", "ID", ID);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            SanPham sp = new SanPham();
            while (reader.Read())
            {
                sp = SanPhamParser.Parse(reader);
                break;
            }
            CloseDB();
            return sp;
        }
    }
}
